# Contributor: lauren n. liberda <lauren@selfisekai.rocks>
# Maintainer: lauren n. liberda <lauren@selfisekai.rocks>
pkgname=mattermost-desktop
pkgver=5.7.0
pkgrel=1
_pkgver="${pkgver/_rc/-rc.}"
pkgdesc="Open source collaboration for developers"
url="https://mattermost.com/"
arch="aarch64 x86_64"	# electron
license="Apache-2.0"
depends="electron"
makedepends="
	electron-dev
	electron-tasje
	nodejs
	npm
	python3
	"
options="net !check"	# broken
checkdepends="libx11-dev make"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/mattermost/desktop/archive/refs/tags/v$_pkgver/mattermost-v$_pkgver.tar.gz

	mattermost-desktop
	"
builddir="$srcdir/desktop-$_pkgver"
replaces="mattermost"

prepare() {
	default_prepare

	git init
	git config user.email x@x
	git config user.name x
	git commit --allow-empty --message "$(date -Is)" --no-gpg-sign

	msg "Downloading dependencies"
	npm ci --ignore-scripts
}

build() {
	msg "Building the app resources"
	NODE_ENV=production npm run build

	msg "Building native modules"
	(
		# it doesn't have an install script with node-gyp and doesn't even have it in deps
		cd node_modules/macos-notification-state
		../.bin/node-gyp rebuild --build-from-source --nodedir=/usr/include/electron/node_headers
	)

	msg "Removing devDependencies"
	npm prune --omit=dev

	msg "Packaging the app"
	tasje --config electron-builder.json pack --additional-asar-unpack '*.node'
}

check() {
	export ELECTRON_OVERRIDE_DIST_PATH=/usr/bin/electron
	npm rebuild robotjs --build-from-source --nodedir=/usr/include/electron/node_headers

	npm run test
}

package() {
	install -Dm644 release/resources/app.asar "$pkgdir"/usr/lib/$pkgname/app.asar
	cp -r release/resources/app.asar.unpacked/ "$pkgdir"/usr/lib/$pkgname/app.asar.unpacked
	install -Dm644 release/Mattermost.Desktop "$pkgdir"/usr/share/applications/$pkgname.desktop
	install -Dm755 "$srcdir"/mattermost-desktop "$pkgdir"/usr/bin/mattermost-desktop

	while read -r size; do
		install -Dm644 release/icons/$size.png "$pkgdir"/usr/share/icons/hicolor/$size/apps/$pkgname.png
	done < release/icons/size-list
}

sha512sums="
b6d0e12e81bc7fa75bba9b4808480db2b64c003e41faf31157612b7f234f76b11334c4fc375adec7b1744358e28e7847e92b78dc26d4e3c10bec860095b15efd  mattermost-desktop-5.7.0.tar.gz
4947e5e7db708f34c812735099c83228b63be7e0f27c19272449c48405a8a49b4f5aa9b45b802c9d6e55a4898ef750d3f9d033e65bb2d0cc906429c5d2864194  mattermost-desktop
"
